//
// 陈日红@2017/5/31 下载基础数据用
//
//

#import "HttpDownload.h"
#import "DlPubEmployee.h"
#import "DlPubDepartment.h"
#import "DlHrScheduling.h"
#import "DIPubQuality.h"
#import "DlHrSatisfaction.h"
#import "DIHTest.h"
@interface HttpDownload()

//
@property NSMutableArray* arrDownloadWhitch;
@property NSInteger completeCount;
@property NSInteger needDownloadCount;
@property NSTimer *myTimer;

@end

@implementation HttpDownload


+(NSArray*) theDownloadItem{
    return [[NSArray alloc]initWithObjects:@"职工档案数据", @"排班数据", @"护理单元数据", @"质量检查相关数据", @"满意度调查相关数据",@"考题相关数据", nil];
}


-(instancetype)initWithDownloadWhich:(NSMutableArray*)arrDownloadWhitch{
    self.arrDownloadWhitch = arrDownloadWhitch;
    self.completeCount = 0;
    self.needDownloadCount = 0;
    for (NSString *val in arrDownloadWhitch) {
        if([val  isEqual: @"1"]){
            self.needDownloadCount++;
        }
    }
    self.isDownloadOver = NO;
    
    return self;
}


-(void) startDownload{

    dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.thothinfo.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);

    BOOL needDownEmployee =  [self.arrDownloadWhitch[0]  isEqual: @"1"];
    BOOL needDownScheduling =  [self.arrDownloadWhitch[1]  isEqual: @"1"];
    BOOL needDownDepartment =  [self.arrDownloadWhitch[2]  isEqual: @"1"];
    BOOL needDownQuality =  [self.arrDownloadWhitch[3]  isEqual: @"1"];
    BOOL needDownSatisfaction =  [self.arrDownloadWhitch[4]  isEqual: @"1"];
    BOOL needDownTest = [self.arrDownloadWhitch[5] isEqual:@"1"];
    if(needDownEmployee){
        dispatch_async(conCurrentQueue, ^{
            [[[DlPubEmployee alloc]init]startDownload:^{
                self.completeCount += 1;
            }];
        });
    }
    
    if(needDownDepartment){
        dispatch_async(conCurrentQueue, ^{
            [[[DlPubDepartment alloc]init]startDownload:^(){
                self.completeCount += 1;
            }];
        });
    }
    
    if(needDownScheduling){
        dispatch_async(conCurrentQueue, ^{
            [[[DlHrScheduling alloc]init]startDownload:^(){
                self.completeCount += 1;
            }];
        });
    }
    
    if(needDownQuality){
        dispatch_async(conCurrentQueue, ^{
            [[[DIPubQuality alloc]init]startDownload:^(){
                self.completeCount += 1;
            }];
        });
    }
    
    if(needDownSatisfaction){
        dispatch_async(conCurrentQueue, ^{
            [[[DlHrSatisfaction alloc]init]startDownload:^(){
                self.completeCount += 1;
            }];
        });
    }
    
    if(needDownTest){
        dispatch_async(conCurrentQueue, ^{
            [[[DIHTest alloc]init]startDownload:^(){
//                self.completeCount += 1;
            }];
        });
    }

    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled:) userInfo:nil repeats:YES];
    

}

- (void)startDownLoadQuality
{
    dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.thothinfo.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(conCurrentQueue, ^{
        [[[DIPubQuality alloc]init]startDownload:^(){
            self.completeCount += 1;
        }];
    });
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled:) userInfo:nil repeats:YES];

}


- (void)startDownExamLoad {
    dispatch_queue_t conCurrentQueue = dispatch_queue_create("com.thothinfo.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(conCurrentQueue, ^{
        [[[DIHTest alloc]init]startDownload:^(){
            self.completeCount += 1;
        }];
    });
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled:) userInfo:nil repeats:YES];

}
-(void)timerCalled:(NSTimer *)timer
{
    // 写不出来 回调 监听属性
    if(self.needDownloadCount == self.completeCount){
        [self.myTimer invalidate];
        self.myTimer = nil;
        
        self.isDownloadOver = YES;
    }
}

@end
